package com.totcy.atom.view.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.totcy.atom.R;
import com.totcy.atom.adapter.CommonFragmentAdapter;
import com.totcy.atom.bean.SubjectBean;
import com.totcy.atom.dialog.SubjectDialog;
import com.totcy.atom.presenter.SubjectPresenter;
import com.totcy.atom.presenter.contract.SubjectContract;
import com.totcy.baselibrary.utils.SPUtil;
import com.totcy.baselibrary.utils.StatusBarUtil;
import com.totcy.baselibrary.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import butterknife.BindView;
import io.reactivex.Observable;


public class RuankaoFragment extends BaseFragment<SubjectContract.View, SubjectPresenter> implements Toolbar.OnMenuItemClickListener, SubjectContract.View, SubjectDialog.OnSubjectClick {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tab_ruankao)
    TabLayout tabRuankao;
    @BindView(R.id.vp_ruankao)
    ViewPager vpRuankao;

    private List<BaseFragment> fragmentList = new ArrayList<>();
    private String[] typeArray = new String[]{"综合", "案例"};
    private String subjectName;
    private List<SubjectBean> subjects = new ArrayList<>();
    private SubjectDialog subjectDialog;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_ruankao;
    }

    @SuppressLint("CheckResult")
    @Override
    public void initView(Bundle savedInstanceState) {
        StatusBarUtil.setPaddingSmart(getActivity(), toolbar);

        subjectName = (String) SPUtil.get(getActivity(), "subjectName", "软件设计师");

        //设置右上角菜单
        toolbar.inflateMenu(R.menu.menu_subject);
        toolbar.setOnMenuItemClickListener(this);
        toolbar.getMenu().getItem(0).setTitle(subjectName);
        toolbar.setTitle(subjectName);

        fragmentList.clear();

        Observable.fromArray(typeArray).subscribe(new Consumer<String>() {
            @Override
            public void accept(String type) throws Exception {
                tabRuankao.addTab(tabRuankao.newTab().setText(type));
                fragmentList.add(new PaperListFragment().setSubjectName(subjectName).setFragmentTitle(type));
            }
        });

        vpRuankao.setAdapter(new CommonFragmentAdapter(getChildFragmentManager(), fragmentList));
        tabRuankao.setupWithViewPager(vpRuankao);
    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        if (subjects.size() > 0) {
            //show
            subjectDialog.showDialog();
        } else {
            subjectDialog = new SubjectDialog(getActivity(), subjects, this);
            showProgress("");
            getPresenter().getSubjectList();
        }
        return true;
    }

    @Override
    public void onSubjectList(List<SubjectBean> subjects) {
        dismissProgress();
        this.subjects.clear();
        this.subjects.addAll(subjects);
        //show
        subjectDialog.initDate();
        subjectDialog.showDialog();
    }

    @Override
    public void onFail(int code, String message) {
        ToastUtil.showToast(getActivity(), message);
    }

    @Override
    public void onItemType(SubjectBean subjectBean) {
        subjectName = subjectBean.getName();
        toolbar.getMenu().getItem(0).setTitle(subjectName);
        toolbar.setTitle(subjectName);

        SPUtil.put(getActivity(), "subjectName", subjectName);

        //更新ViewPager 里的fragment 数据
        for (BaseFragment fragment : fragmentList) {
            ((PaperListFragment) (fragment)).setSubjectName(subjectName).refresh();
        }
    }
}
